package assurbike;

import java.util.List;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class ApiAssurBike
{
	private static String getCylindrees(int marque)
	{
		String adresse = "http://www.assurbike.com/estimation/new/loadoptions/fr/list/cylindrees?str="
				+ marque;
		return UtilHttp.doPost(adresse, "");
	}

	private static String getTypes(String cylindree)
	{
		String adresse = "http://www.assurbike.com/estimation/new/loadoptions/fr/list/modeles?str="
				+ cylindree;
		return UtilHttp.doPost(adresse, "");
	}

	public static String etape1(Integer marque, String cylindree, String type,
			String dateAchat, String dateImmat)
	{
		String adresse = "http://www.assurbike.com/estimation/new/etapevalidation/fr/";
		String data2 = "etape=1&type_veh=1&marque="
				+ marque
				+ "&cylindree="
				+ cylindree
				+ "&modele="
				+ type
				+ "&code_ant=1&bike_type=3&quadro_type=1&quadro_cost=1&cgf=1&tcg=1&cgs=0&buying_date="
				+ dateAchat // 30%2F07%2F2012
				+ "&date_mc="
				+ dateImmat // 01%2F06%2F2011
				+ "&age_veh=&code_postal=91250&commune=38384&parking=0&usage=0";
		return UtilHttp.doPost(adresse, data2);
	}

	public static String etape2()
	{
		String adresse = "http://www.assurbike.com/estimation/new/etapevalidation/fr/";
		String data2 = "etape=2&femme=0&lastname=fdsdfsdfs&firstname=dfsdfsdfs&adress1=&adress2=&email=pangopango%40yopmail.com&telephone=01-02-03-04-05&birthdate=01%2F07%2F1985&date_obtention=01%2F04%2F2010&age_permis=2.3118&type_permis=a&driving_experience=1&europ=1";
		return UtilHttp.doPost(adresse, data2);
	}

	public static String etape3()
	{
		String adresse = "http://www.assurbike.com/estimation/new/etapevalidation/fr/";
		String data2 = "etape=3&dune_condamnation=0&dune_condamnation_date=&dune_condamnation_yes=1&dune_condamnation_yes_3=0&dune_condamnation_yes_4=0&dune_condamnation_two=0&dune_suspension=0&dune_suspension_duration=1&dune_suspension_motive=1&dune_suspension_motive_descr=&dune_annulation=0&dune_annulation_motive=1&dune_annulation_motive_descr=&dune_nonpayment=0&dune_resiliation=0&dune_resiliation_motive_choice_1=0&dune_resiliation_motive_choice_2=0&dune_resiliation_motive_choice_3=0&dune_resiliation_motive_choice_4=0&dune_resiliation_motive_choice_5=0&h_question=1&crm=100&auto=1&auto_crm=100&driver=1&sin_resp_auto=0&sin_nonresp_auto=0&sin_resp_moto=0&sin_resp_moto_corp_num=&sin_nonresp_moto=0&vol_moto=0&promocode=";
		return UtilHttp.doPost(adresse, data2);
	}

	public static String calculate()
	{
		String adresse = "http://www.assurbike.com/estimation/new/loadoptions/fr/list/calculate";
		String data = "etape=4&customer_id=&opt%5B%5D=10&plan=0&";
		return UtilHttp.doPost(adresse, data);
	}

	public static void init()
	{
		String adresse = "http://www.assurbike.com/estimation/new/index/fr/veh/1";
		List<String> cookie = UtilHttp.getPropertyResponse(adresse,
				"Set-Cookie");

		UtilHttp.PhpSessId = cookie.get(0).split(";")[0];
		System.out.println("UtilHttp.PhpSessId = " + UtilHttp.PhpSessId);
	}

	public static TreeMap<String, String> getCylindreesMap(int numeroMarque)
	{
		String listCylindrees = ApiAssurBike.getCylindrees(numeroMarque);
		listCylindrees = listCylindrees.substring(13).substring(0,
				listCylindrees.length() - 58);
		StringTokenizer tokenizer = new StringTokenizer(listCylindrees, ",");

		TreeMap<String, String> cylindrees = new TreeMap<String, String>();

		while (tokenizer.hasMoreTokens())
		{
			String value = tokenizer.nextToken();
			String display = tokenizer.nextToken();
			value = value.substring(16, value.length() - 1);
			display = display.substring(16, display.length() - 1);
			cylindrees.put(display, value);
		}
		return cylindrees;
	}

	public static TreeMap<String, String> getTypesMap(String cylindree)
	{
		String listTypes = ApiAssurBike.getTypes(cylindree);
		listTypes = listTypes.substring(13).substring(0,
				listTypes.length() - 58);
		StringTokenizer tokenizer = new StringTokenizer(listTypes, ",");
		TreeMap<String, String> types = new TreeMap<String, String>();
		while (tokenizer.hasMoreTokens())
		{
			String value = tokenizer.nextToken();
			String display = tokenizer.nextToken();
			value = value.substring(16, value.length() - 1);
			display = display.substring(16, display.length() - 1);
			types.put(display, value);
		}
		return types;
	}
}
